<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Listexport Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Listexport_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_infobill';
	}
        
        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'int',
						)
				),
				array(
						'name' => 'customerID',
						'label' => $this->lang->line('customerID'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),array(
						'name' 	=> 'fare',
						'label' => $this->lang->line('exportBy'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),
				array(
						'name' 	=> 'infoBill',
						'label' => $this->lang->line('infoBill'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'int',
						)
				),
                                array(
						'name' 	=> 'save_contact',
						'label' => $this->lang->line('save_contact'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),array(
						'name' 	=> 'owner',
						'label' => $this->lang->line('owner'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => 'DESC',
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("usr.username,bill.id,bill.customerID,bill.fare,bill.infoBill,bill.save_contact,bill.lastupdated")
		->from($this->table_name.' AS bill' )
                ->join('users AS usr','usr.id = bill.owner');
		//->unset_column('id');
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
					$item['id'],
					$item['customerID'],
					($item['fare']==0)?'Giá net':'Giá thực',
                                        $item['infoBill'],
                                        ($item['save_contact']==1)?'Có':'Không',
                                        $item['username'],
                                        date("d-m-Y H:i:s",strtotime($item['lastupdated']))
			);
		}
	
		return json_encode($ouput);
	}

}